import java.util.ArrayList;
import java.util.Scanner;

/**
 * This program checks that an entered string
 * is an anagram of an another entered string
 *
 * @author Sergii Koziumynskyi
 * @version 0.01 2014-01-16
 */

public class Anagram {
    public static void main(String[] args) {

        // ask user about input and convert it to expected format
        String firstDirtyInput = getStringFromUser();
        String secondDirtyInput = getStringFromUser();
        ArrayList<String> firstString = convertStringToArrayListRemovingSpaces(firstDirtyInput);
        ArrayList<String> secondString = convertStringToArrayListRemovingSpaces(secondDirtyInput);

        // main calculations
        boolean missedLetter = false;
        boolean emptyFirstInput = false;
        boolean emptySecondInput = false;
        if (firstString.isEmpty()) {
            emptyFirstInput = true;
        } else if (secondString.isEmpty()) {
            emptySecondInput = true;
        } else {
            missedLetter = checkCharsInArrayListsAreAnagrams(firstString, secondString);
        }

        // print results
        if (emptyFirstInput) {
            System.out.println("First string is empty. Execution terminated.");
        } else if (emptySecondInput) {
            System.out.println("Second string is empty. Execution terminated.");
        } else if (missedLetter) {
            System.out.println("Entered strings are NOT anagrams");
        } else if (secondString.size() > 0) {
            System.out.println("Entered strings will be anagrams " +
                    "if you remove following redundant letter(s) from second string: " + secondString);
        } else {
            System.out.println("Entered strings are anagrams");
        }
    }

    /**
     * Method gets two ArrayLists where each element is String representation of a character and
     * removes all letters from first ArrayList in second ArrayList.
     *
     * @param firstString first ArrayList<String> with chars
     * @param secondString second ArrayList<String> with chars
     * @return
     *          boolean 'true' if all letters from firstString are found in the secondString
     *          boolean 'false' if a letter(s) from firstString is not found in the secondString
     */
    private static boolean checkCharsInArrayListsAreAnagrams(ArrayList<String> firstString,
                                                             ArrayList<String> secondString) {
        boolean result = false;
        for (String aFirstString : firstString) {
            if (result) {
                break;
            } else {
                for (int j = 0; j < secondString.size(); j++) {
                    if (aFirstString.equals(secondString.get(j))) // letter found; stop search of current letter and seek next
                    {
                        secondString.remove(j);
                        break;
                    } else if (j == secondString.size() - 1) // a searched letter is missed; strings are not anagrams; stop search;
                    {
                        result = true;
                        break;
                    } //else
                }
            }
        }
        return result;
    }

    /**
     * Method gets a string and converts it to ArrayList<String> with all chars from the string except spaces
     *
     * @param dirtyInput a string
     * @return ArrayList which contains all letters from dirtyInput except spaces
     */
    private static ArrayList<String> convertStringToArrayListRemovingSpaces(String dirtyInput) {
        ArrayList<String> result = new ArrayList<String>();
        for (int i = 0; i < dirtyInput.length(); i++) {
            String z = dirtyInput.substring(i, ++i);
            i--;
            if (!z.equals(" ")) {
                result.add(z);
            }
        }
        return result;
    }

    /**
     * Method requests an input from user
     *
     * @return the entered string, trimmed and all symbols are in lower case
     */
    private static String getStringFromUser() {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a string using Latin characters.");
        return in.nextLine().trim().toLowerCase();
    }
}